#!/bin/bash

#$1  status     --> enable|disable
#$2  localdir   --> local code dir
#$3  codeowner  --> code owner and group
#$4  remotecode --> remote mount code
#$5  ignorelist --> ignore file
#$6  node	--> node name ip address

# check code 
dir='/tmp/runlog_check'
templog="$dir/checktmp.txt"
shiled="$dir/shiled.txt"
alllog="$dir/checkall.txt"
cplist="$dir/checkcp.txt"
rmlist="$dir/checkrm.txt"
chlist="$dir/checkch.txt"
output="$dir/runlog.txt"
host=`basename $4`

[ -d $2 ] || mkdir -p $2
rm -rf $dir && mkdir $dir
remotedir=$4
echo "$4" |sed 's/\//\\\//g' > $templog
aliasremotedir=`cat $templog`
echo "$2" |sed 's/\//\\\//g' > $templog
aliaslocaldir=`cat $templog`
localdir=$2

fun_main() {
    if [ -z $1 ];then
        touch $shiled
    else
        for i in `echo $1|sed 's/,/ /g'`;do
            echo -e "${remotedir}$i\n${localdir}$i" >> $shiled
        done
    fi
    diff -rq $remotedir $localdir -x '.git' > $templog
    if [ $? -eq 0 ] || [ $? -eq 1 ];then
        sed 's/: /\//' $templog |grep -v -f $shiled > $alllog
    	grep "^Only in $remotedir" $alllog |sed 's/^Only in '"$aliasremotedir"'//' > $cplist
    	grep "^Only in $localdir" $alllog | sed 's/^Only in '"$aliaslocaldir"'//' > $rmlist
    	grep "^Files " $alllog |sed 's/^Files '"$aliasremotedir"'//;s/ and .*//' > $chlist 
    else
	echo -e "\033[31m执行对比文件发生错误！\033[0m"
	exit 
    fi
}
fun_main_cp() {
    if [ -s $cplist ];then
        cat $cplist|while read i;do
            $1
            [ $? -eq 0 ] && echo "新增文件【成功】：$i" >> $output || echo "新增文件【失败】：$i" >> $output
        done
    else
        echo "新增文件：无" >> $output
    fi
}
fun_main_rm() {
    if [ -s $rmlist ];then
        cat $rmlist|while read i;do
            $1
            [ $? -eq 0 ] && echo "删除文件【成功】：$i" >> $output || echo "删除文件【失败】：$i" >> $output
        done
    else
        echo "删除文件：无" >> $output
    fi
}
fun_main_ch() {
    if [ -s $chlist ];then
        cat $chlist|while read i;do
            $1
            [ $? -eq 0 ] && echo "更新文件【成功】：$i" >> $output || echo "更新文件【失败】：$i" >> $output
        done
    else
        echo "更新文件：无" >> $output
    fi
}
fun_cp() { 
    cp -r ${remotedir}${i} ${localdir}${i}
}
fun_rm() { 
    rm -rf ${localdir}${i} 
}
fun_ch() { 
    \cp -f ${remotedir}${i} ${localdir}${i} 
}
fun_null() {
    [ 1 -eq 1 ]
}
oldifs=$IFS
if [[ $1 == 'update' ]] || [[ $1 == 'rollback' ]];then
    fun_main $5
    IFS=$(echo -en "\n\b")
    fun_main_cp fun_cp
    fun_main_rm fun_rm
    fun_main_ch fun_ch
    IFS=$oldifs
    chown -R $3.$3 $localdir
else
    fun_main $5
    IFS=$(echo -en "\n\b")
    fun_main_cp fun_null
    fun_main_rm fun_null
    fun_main_ch fun_null
    IFS=$oldifs
fi
#format repot 
logformat() {
    tempfile='/tmp/runlog_check/temp_Yxj3x.txt'
    #sed -n '/^新增/,/^executed/p' $1 |tac|sed '1d'|tac|grep -v "无" > $tempfile
    grep -v "无" $1 > $tempfile
    cpall=`grep -c "新增" $tempfile`
    cpbad=`grep "新增" $tempfile |grep -c "失败"`
    cpok=`grep "新增" $tempfile|grep -c "成功"`
    rmall=`grep -c "删除" $tempfile`
    rmbad=`grep "删除" $tempfile|grep -c "失败"`
    rmok=`grep "删除" $tempfile|grep -c "成功"`
    chall=`grep -c "更新" $tempfile`
    chbad=`grep "更新" $tempfile|grep -c "失败"`
    chok=`grep "更新" $tempfile|grep -c "成功"`
    fun1(){
        echo -n "$2:"
        for i in `grep "$1" $tempfile|sed 's/^.*：/  /'`;do
            echo -n "'$i',"
        done
    }
    echo -n "time:"
    date '+%F %H:%M:%S'
    echo -n "node:"
    echo $4
    echo -n "project:"
    echo $host
    echo -n "path:"
    echo $3
    echo -n "mode:"
    if [[ $2 == 'update' ]];then
	echo -e "\033[31m更新\033[0m"
    elif [[ $2 == 'rollback' ]];then
	echo -e "\033[31m回滚\033[0m"
    else
	echo -e "\033[32m检测\033[0m"
    fi
    IFS=$(echo -en "\n\b")
    fun1 "新增" addlist
    echo 
    fun1 "删除" rmlist
    echo  
    fun1 "更新" chlist
    IFS=$oldifs
    if [[ $2 == 'update' ]];then
        echo
        echo "addcount:$cpall"
        echo "addsucess:$cpok"
        echo "addfailed:$cpbad"
        echo "rmcount:$rmall"
        echo "rmsucess:$rmok"
        echo "rmfailed:$rmbad"
        echo "chcount:$chall"
        echo "chsucess:$chok"
        echo -n "chfailed:$chbad"
    fi
}

logformat $output $1 $2 $6
